﻿using Exam.infranstuctrue.Dto.Request.Exam;
using Exam.Service.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Exam.Controllers
{
    [ApiController]
    public class TestPaperController : BaseController
    {
        private readonly ITestPaperServer _testPaper;

        public TestPaperController(ITestPaperServer testPaper)
        {
            _testPaper = testPaper;
        }
        /// <summary>
        /// 获取试题列表
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="paeg"></param>
        /// <param name="key"></param>
        /// <param name="IsPublic"></param>
        /// <param name="IsBan"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetTestPaper(int page = 1, int limit = 10, string? key = "", int IsPublic = -1, int IsBan = -1)
        {
            key = key ?? "";
            var result = await _testPaper.GetTestPaper(page, limit, key, IsPublic, IsBan);
            return Ok(result);
        }
        /// <summary>
        /// 组卷 --随机
        /// </summary>
        /// <param name="count"></param>
        /// <param name="minLv"></param>
        /// <param name="maxLv"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ConstituteTestPage(int count, int minLv, int maxLv)
        {
            var result = await _testPaper.ConstituteTestPage(count, minLv, maxLv);
            return Ok(result);
        }
        /// <summary>
        /// 组卷 -人工
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ArtificialTestPage(int page = 1, int limit = 10, string? key = "")
        {
            key = key ?? "";
            var result = await _testPaper.ArtificialTestPage(page, limit, key);
            return Ok(result);
        }
        /// <summary>
        /// 添加试卷
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> RandomAddTestPage(string? jsonStr)
        {

            var dto = JsonConvert.DeserializeObject<AddTestPageDto>(jsonStr);
            var result = await _testPaper.RandomAddTestPage(dto);
            return Ok(result);
        }
        /// <summary>
        /// 删除试卷
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> DeleteTestPaper(List<AddTestPageDto> dto)
        {
            var result = await _testPaper.DeleteTestPaper(dto);
            return Ok(result);
        }
        /// <summary>
        /// 获取单个试卷
        /// </summary>
        /// <param name="tpid"></param>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetEchoTestPaper(string tpid, int page = 1, int limit = 10, string? key = "")
        {
            key = key ?? "";
            var result = await _testPaper.GetEchoTestPaper(tpid, page, limit, key);
            return Ok(result);
        }
        /// <summary>
        /// 修改试卷
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateTestPaper(AddTestPageDto dto)
        {
            //AddTestPageDto dto
            //var dto = JsonConvert.DeserializeObject<AddTestPageDto>(str);
            var result = await _testPaper.UpdateTestPaper(dto);
            return Ok(result);
        }
        /// <summary>
        /// 是否发布
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> IsPublic(List<AddTestPageDto> dto)
        {
            var result = await _testPaper.IsPublic(dto);
            return Ok(result);
        }
        /// <summary>
        /// 是否禁用
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> IsBan(List<AddTestPageDto> dto)
        {
            var result = await _testPaper.IsBan(dto);
            return Ok(result);
        }
    }
}
